#include <types.h>
#include <osal/osal.h>
#include <api/libc/printf.h>
#include <api/libc/string.h>
#include <api/libosd/osd_lib.h>
#include <api/libpub/lib_frontend.h>
#include "osdobjs_def.h"

#include "osd_config.h"
#include "string.id"
#include "images.id"
#include "menus_root.h"
#include "win_com.h"

#ifdef SUPPORT_CAS_A
#include "win_com_menu_define.h"
#endif

#define FRONT_END_NUM   2

#define WIN_SH_IDX	  WSTL_WIN_BODYRIGHT_01_HD
#define WIN_HL_IDX	  WSTL_WIN_BODYRIGHT_01_HD
#define WIN_SL_IDX	  WSTL_WIN_BODYRIGHT_01_HD
#define WIN_GRY_IDX  WSTL_WIN_BODYRIGHT_01_HD

#define CON_SH_IDX   WSTL_BUTTON_01_HD
#define CON_HL_IDX   WSTL_BUTTON_02_HD
#define CON_SL_IDX   WSTL_BUTTON_01_HD
#define CON_GRY_IDX  WSTL_BUTTON_07_HD

#define TXTI_SH_IDX   WSTL_BUTTON_01_FG_HD
#define TXTI_HL_IDX   WSTL_BUTTON_02_FG_HD
#define TXTI_SL_IDX   WSTL_BUTTON_01_FG_HD
#define TXT_GRY_IDX  WSTL_BUTTON_07_HD

#define TXTN_SH_IDX   WSTL_BUTTON_01_FG_HD
#define TXTN_HL_IDX   WSTL_BUTTON_02_FG_HD
#define TXTN_SL_IDX   WSTL_BUTTON_01_FG_HD
#define TXTN_GRY_IDX  WSTL_BUTTON_07_HD


#define TXTC_SH_IDX   WSTL_BUTTON_01_FG_HD
#define TXTC_HL_IDX   WSTL_BUTTON_02_FG_HD
#define TXTC_SL_IDX   WSTL_BUTTON_01_FG_HD
#define TXTC_GRY_IDX  WSTL_BUTTON_07_HD

#define LIST_BAR_SH_IDX  WSTL_SCROLLBAR_01_8BIT//sharon WSTL_SCROLLBAR_01_HD
#define LIST_BAR_HL_IDX  WSTL_SCROLLBAR_01_8BIT//sharon WSTL_SCROLLBAR_01_HD

#define LIST_BAR_MID_RECT_IDX  	WSTL_NOSHOW_IDX
#define LIST_BAR_MID_THUMB_IDX  	WSTL_SCROLLBAR_02_8BIT//sharon WSTL_SCROLLBAR_02_HD

#ifndef SD_UI
#if Maker_GUI_ON
#ifndef SUPPORT_CAS_A
/* define in win_com_menu_define.h already */
#define	W_L     	Maker_GUI_SM_L
#define	W_T     	Maker_GUI_SM_T
#define	W_W     	Maker_GUI_SM_W
#define	W_H     	Maker_GUI_SM_H
#endif
#else
#ifndef SUPPORT_CAS_A
/* define in win_com_menu_define.h already */
#define	W_L     	248
#define	W_T     	98
#define	W_W     	692
#define	W_H     	488
#endif
#endif

#define SCB_L 		(W_L + 20)
#define SCB_T 		110//150
#define SCB_W 		12//sharon 18
#define SCB_H 		460

#if Maker_GUI_ON
#define CON_L		(W_L + 20)
#define CON_T		(W_T + 4)
#define CON_W		(W_W - 72 - (SCB_W + 4))
#define CON_H		40
#define CON_GAP 	12
#else
#define CON_L		(SCB_L + SCB_W + 10)
#define CON_T		(W_T + 4)
#define CON_W		(W_W - 72 - (SCB_W + 4))
#define CON_H		40
#define CON_GAP 	12
#endif

#define TXTN_L_OF	10
#define TXTN_T_OF  	0
#define TXTN_W		(CON_W-20)
#define TXTN_H     	40

#define LINE_L_OF	0
#define LINE_T_OF  	(CON_H+4)
#define LINE_W		CON_W
#define LINE_H     	4
#else
#define	W_L     	105
#define	W_T     	57
#define	W_W     	482
#define	W_H     	370

#define SCB_L 		(W_L + 20)
#define SCB_T 		110//150
#define SCB_W 		12//sharon 18
#define SCB_H 		360

#define CON_L		(SCB_L + SCB_W )
#define CON_T		(W_T + 4)
#define CON_W		(W_W - 30 - (SCB_W + 4))
#define CON_H		32
#define CON_GAP 	8

#define TXTN_L_OF	10
#define TXTN_T_OF  	0
#define TXTN_W		(CON_W-20)
#define TXTN_H     	30

#define LINE_L_OF	0
#define LINE_T_OF  	(CON_H+4)
#define LINE_W		CON_W
#define LINE_H     	4
#endif

extern CONTAINER   win_ota_combo;

extern CONTAINER   combo_ota_con1; 
extern CONTAINER   combo_ota_con2;


extern TEXT_FIELD   combo_ota_txt1; 
extern TEXT_FIELD   combo_ota_txt2;



static PRESULT combo_item_con_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);
static VACTION combo_item_con_keymap(POBJECT_HEAD pObj, UINT32 key);

static VACTION combo_ota_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT combo_ota_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);


#define LDEF_CON(root, varCon,nxtObj,ID,IDl,IDr,IDu,IDd,l,t,w,h,conobj,focusID)		\
    DEF_CONTAINER(varCon,root,nxtObj,C_ATTR_INACTIVE,0, \
    ID,IDl,IDr,IDu,IDd, l,t,w,h, CON_SH_IDX,CON_HL_IDX,CON_SL_IDX,CON_GRY_IDX,   \
    combo_item_con_keymap,combo_item_con_callback,  \
    conobj, ID,1)

    
#define LDEF_TXTNAME(root,varTxt,nxtObj,ID,IDl,IDr,IDu,IDd,l,t,w,h,resID,str)		\
    DEF_TEXTFIELD(varTxt,root,nxtObj,C_ATTR_INACTIVE,0, \
    ID,IDl,IDr,IDu,IDd, l,t,w,h, TXTN_SH_IDX,TXTN_HL_IDX,TXTN_SL_IDX,TXTN_GRY_IDX,   \
    NULL,NULL,  \
    C_ALIGN_LEFT | C_ALIGN_VCENTER, 10,0,resID,str)

#define LDEF_MM_ITEM(root,varCon,nxtObj,varTxtName,ID,IDu,IDd,l,t,w,h,nameID)	\
	LDEF_CON(&root,varCon,nxtObj,ID,ID,ID,IDu,IDd,l,t,w,h,&varTxtName,1)	\
	LDEF_TXTNAME(&varCon,varTxtName,NULL,1,1,1,1,1,l + TXTN_L_OF, t + TXTN_T_OF,TXTN_W,TXTN_H,nameID,NULL)	


LDEF_MM_ITEM(win_ota_combo,combo_ota_con1,&combo_ota_con2,combo_ota_txt1,1,2,2,CON_L, CON_T + (CON_H + CON_GAP)*0   \
            ,CON_W,CON_H,0)

LDEF_MM_ITEM(win_ota_combo,combo_ota_con2,NULL,combo_ota_txt2,2,1,1,CON_L, CON_T + (CON_H + CON_GAP)*1   \
            ,CON_W,CON_H,0)




DEF_CONTAINER(win_ota_combo,NULL,NULL,C_ATTR_ACTIVE,0, \
    1,0,0,0,0, W_L,W_T,W_W,W_H, WIN_SH_IDX,WIN_HL_IDX,WIN_SL_IDX,WIN_GRY_IDX,   \
    combo_ota_keymap,combo_ota_callback,  \
    &combo_ota_con1, 1,0)
    

TEXT_FIELD *text_array[] = 
{
    &combo_ota_txt1,
    &combo_ota_txt2,
};

/*******************************************************************************
*	Local vriable & function declare
*******************************************************************************/

void combo_ota_ui_option_name_set(void);
void combo_ota_enter_ota_uprade(POBJECT_HEAD pobj);
UINT8 combo_ota_ui_config_frontend(void);
/*******************************************************************************
*	key mapping and event callback definition
*******************************************************************************/
static VACTION combo_item_con_keymap(POBJECT_HEAD pObj, UINT32 key)
{
    VACTION ret= VACT_PASS;

    switch(key)
    {
        case V_KEY_ENTER:
            ret = VACT_ENTER;
            break;
        default:
            break;
    }
    return ret;
        
}
static PRESULT combo_item_con_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
    PRESULT ret = PROC_PASS;
    switch(event)
    {
        case EVN_UNKNOWN_ACTION:
            combo_ota_enter_ota_uprade(pObj);
            break;
        default:
            break;
    }
    return ret;    
}
static VACTION combo_ota_keymap(POBJECT_HEAD pObj, UINT32 key)
{

    VACTION ret= VACT_PASS;

    switch(key)
    {
        case V_KEY_EXIT:
        case V_KEY_MENU://ChengYu@20131004, TT#1470
            ret = VACT_CLOSE;
            break;
        case V_KEY_UP:
            ret = VACT_CURSOR_UP;
            break;
        case V_KEY_DOWN:
            ret = VACT_CURSOR_DOWN;
            break;
        default:
        break;
    }
    return ret;
}
static PRESULT combo_ota_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
    PRESULT ret = PROC_PASS;
    POBJECT_HEAD    pfocusobj;
    switch(event)
    {
        case EVN_PRE_OPEN:
	     	wincom_open_title(pObj,RS_TOOLS_SW_UPGRADE_BY_OTA, 0);
            combo_ota_ui_option_name_set();
            pfocusobj = OSD_GetFocusObject(pObj);
            if(pfocusobj != NULL)
            {            
                if(!OSD_CheckAttr(pfocusobj,C_ATTR_ACTIVE))
                {
                    pfocusobj = OSD_GetAdjacentObject(pfocusobj,VACT_CURSOR_DOWN);
                    if(pfocusobj != NULL && OSD_CheckAttr(pfocusobj,C_ATTR_ACTIVE) )
                        OSD_ChangeFocus(pObj,pfocusobj->bID,0);                  
                        
                }
            }            
            break;
        case EVN_POST_OPEN:
            break;
        case EVN_PRE_CLOSE:
            *((UINT32*)param2) &= ~C_CLOSE_CLRBACK_FLG;	
            break;
        case EVN_POST_CLOSE:    
            break;
        default:
            break;
    }
    return ret;    
}


void combo_ota_ui_option_name_set(void)
{
    INT i;
    UINT8 fe_type;
    UINT8 front_end_type_num;
    BOOL  active_flag = FALSE;
    
    
	SYSTEM_DATA *sys_data = sys_data_get();

    if(sys_data->antenna_connect_type == ANTENNA_CONNECT_DUAL_DIFF)
        front_end_type_num = 2;
    else
        front_end_type_num = 1;

    front_end_type_num = combo_ota_ui_config_frontend();
    if(front_end_type_num == 1)
        remove_menu_item(&win_ota_combo, (OBJECT_HEAD*)&combo_ota_con2, CON_H + CON_GAP);
    
    for(i = 0;i < front_end_type_num;i++)
    {
        text_array[i]->head.pRoot->bAttr = C_ATTR_ACTIVE;
        text_array[i]->head.bAttr = C_ATTR_ACTIVE;
        
        fe_type = board_get_frontend_type(i);
        switch(fe_type)
        {
            case FRONTEND_TYPE_S:
                text_array[i]->wStringID = RS_COMBO_OTA_S;
                if(get_tuner_sat_cnt(TUNER_EITHER) == 0)
                {
                    text_array[i]->head.pRoot->bAttr = C_ATTR_INACTIVE;
                    text_array[i]->head.bAttr = C_ATTR_INACTIVE;
                }
                else
                {                    
                    text_array[i]->head.pRoot->bAttr = C_ATTR_ACTIVE;
                    text_array[i]->head.bAttr = C_ATTR_ACTIVE;
                    active_flag = TRUE;
                }
                break;
            case FRONTEND_TYPE_T:
                if(board_frontend_is_isdbt())
                    text_array[i]->wStringID = RS_COMBO_OTA_ISDBT;
                else
                    text_array[i]->wStringID = RS_COMBO_OTA_T;
                active_flag = TRUE;
                break;
            case FRONTEND_TYPE_C:
                text_array[i]->wStringID = RS_COMBO_OTA_C;
                active_flag = TRUE;
                break;                
            default:
                break;
        }
    }
    if(!active_flag)
        win_ota_combo.FocusObjectID = 0;

    if(win_ota_combo.FocusObjectID == 0 && active_flag)
        win_ota_combo.FocusObjectID = 1;
    
}


UINT8 combo_ota_ui_config_frontend(void)
{
    INT32 i;
    UINT8 count=0;
    UINT8 s_front = 0,t_front = 0,isdbt_front = 0,c_front = 0;
    
    for(i = 0; i < FRONT_END_NUM; i++)
    {
        switch(board_get_frontend_type(i))
        {
            case FRONTEND_TYPE_S:
                s_front++;
                break;
            case FRONTEND_TYPE_T:
                if(board_frontend_is_isdbt(i))
                    isdbt_front++;
                else
                    t_front++;
                break;
            case FRONTEND_TYPE_C:
                c_front++;
                break;                
            default:
                break;
        }  
    }

    if(s_front == 2 || t_front == 2|| isdbt_front == 2|| c_front == 2 || (s_front + t_front + isdbt_front + c_front == 1))
        count = 1;
    else
    {
        if(s_front + t_front + isdbt_front + c_front == 2)
        {
            count = 2;
        }
    }
    
    return count;
}



void combo_ota_enter_ota_uprade(POBJECT_HEAD pobj)
{
    PCONTAINER pcon ,pwin;
    UINT32     param;

    pwin = NULL;
    pcon = (PCONTAINER)pobj;
    pwin = &win_otaupg_con;
    param = MENU_OPEN_TYPE_MENU;
    
    if(((PTEXT_FIELD)(pcon->pNextInCntn))->wStringID == RS_COMBO_OTA_S)
    {
        param |= FRONTEND_TYPE_S;
    }
    else if(((PTEXT_FIELD)(pcon->pNextInCntn))->wStringID == RS_COMBO_OTA_T)
    {
        param |= FRONTEND_TYPE_T;
    }
    else if(((PTEXT_FIELD)(pcon->pNextInCntn))->wStringID == RS_COMBO_OTA_ISDBT)
    {
        param |= FRONTEND_TYPE_ISDBT;
    }
    else if(((PTEXT_FIELD)(pcon->pNextInCntn))->wStringID == RS_COMBO_OTA_C)
    {
        param |= FRONTEND_TYPE_C;
    }
    
	if(OSD_ObjOpen((POBJECT_HEAD)pwin, param) != PROC_LEAVE)
			menu_stack_push((POBJECT_HEAD)pwin);

        
}
